package com.snail.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.snail.model.dto.ManageQuesstionDto;
import com.snail.model.dto.TestQuestionDTO;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizQuestionService;
import com.snail.model.pojo.BizQuestion;
import com.snail.mapper.BizQuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author Monica
* @description 针对表【biz_question(生成问题（非选择）表)】的数据库操作Service实现
* @createDate 2025-07-16 10:22:25
*/
@Service
public class BizQuestionServiceImpl extends ServiceImpl<BizQuestionMapper, BizQuestion>
    implements BizQuestionService {
    @Autowired
    QuestionByAiService questionByAiService;

    public BaseResponse<?> addQuestion(ManageQuesstionDto dto) {
        BizQuestion question = new BizQuestion();
        BeanUtils.copyProperties(dto, question);
        // 设置创建人等字段
        question.setCreateTime(LocalDateTime.now());
        question.setCreateTime(LocalDateTime.now());
        question.setCreateBy(StpUtil.getLoginIdAsInt());
        question.setUpdateBy(StpUtil.getLoginIdAsInt());
        this.save(question);
        return ResultUtils.success("题目添加成功");
    }


    public BaseResponse<?> deleteQuestion(Long id) {
        boolean removed = this.removeById(id);
        return removed ? ResultUtils.success("题目删除成功") : ResultUtils.error("题目删除失败");
    }


    public BaseResponse<?> updateQuestion(Long id, ManageQuesstionDto dto) {
        BizQuestion question = this.getById(id);
        if (question == null) {
            return ResultUtils.error("题目不存在");
        }
        BeanUtils.copyProperties(dto, question);
        question.setId(Math.toIntExact(id));
        this.updateById(question);
        return ResultUtils.success("题目更新成功");
    }


    public BaseResponse<?> listQuestions(Integer questionType,Integer questionTag, Integer page, Integer size) {
        //System.out.println("type:"+questionType+"tag:"+questionTag);
        QueryWrapper<BizQuestion> wrapper = new QueryWrapper<>();
        if (questionType != null) {
            wrapper.eq("question_type", questionType);
        }
        if (questionTag != null) {
            wrapper.eq("question_tags", questionTag);
        }

        Page<BizQuestion> questionPage = new Page<>(page, size);
        this.page(questionPage, wrapper);
        // 数据库是否已经存在题目
        if (questionPage.getRecords() == null || questionPage.getRecords().isEmpty()) {
            try {
                // 使用ai生成的题目
                List<BizQuestion> aiQuestions = questionByAiService.chat(questionType, questionTag);
                return ResultUtils.success(aiQuestions);
            } catch (JsonProcessingException e) {
                // 生成题目失败，抛出异常
                return ResultUtils.success(questionPage);
            }
        }

        return ResultUtils.success(questionPage);
    }


    public BaseResponse<?> getQuestionById(Long id) {
        BizQuestion question = this.getById(id);
        return question != null ? ResultUtils.success(question) : ResultUtils.error("题目未找到");
    }

}




